package com.bwie.product.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.bwie.common.core.constant.Constants;
import com.bwie.common.core.domain.Attrs;
import com.bwie.common.core.domain.PageResult;
import com.bwie.common.core.domain.Result;
import com.bwie.common.core.domain.SkuEsModel;
import com.bwie.product.domain.SkuInfo;
import com.bwie.product.domain.dto.SpuInfoDTO;
import com.bwie.product.domain.po.PmsBrandPO;
import com.bwie.product.domain.po.PmsCategoryPO;
import com.bwie.product.domain.po.PmsProductAttrValuePO;
import com.bwie.product.domain.vo.SpuInfoVO;
import com.bwie.product.mapper.*;
import com.bwie.product.service.SkuInfoService;
import com.bwie.product.service.SpuInfoMerchantService;
import com.bwie.system.api.RemoteSearchService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SpuInfoMerchantServiceImpl implements SpuInfoMerchantService {
    @Autowired
    private SpuInfoMerchantMapper spuInfoMerchantMapper;

    @Autowired
    private SkuInfoService skuInfoService;

    @Autowired
    private SkuInfoMapper skuInfoMapper;

    @Autowired
    private PmsProductAttrValueMapper productAttrValueMapper;

    @Autowired
    private PmsBrandMapper pmsBrandMapper;

    @Autowired
    private PmsCategoryMapper pmsCategoryMapper;

    @Autowired
    private RemoteSearchService remoteSearchService;

    /**
     * 商户端
     * 获取spu列表
     */
    @Override
    public PageResult<SpuInfoVO> getSpuInfoList(SpuInfoDTO spuInfoDTO) {
        //调用分页工具
        PageHelper.startPage(spuInfoDTO.getPageNum(),spuInfoDTO.getPageSize());
        List<SpuInfoVO> list = spuInfoMerchantMapper.getSpuInfoList(spuInfoDTO);
        PageInfo<SpuInfoVO> pageInfo = new PageInfo<>(list);
        return PageResult.toPageResult(pageInfo.getTotal(),list);
    }

    /**
     * 商家端
     * 商品上下架
     * @return
     */
    @Override
    public void upperOrLowerShelves(Long id, Integer publishStatus) {
        if (publishStatus==0){
            //商品下架
            //远程调用根据spuId删除es中的sku信息方法
            Result result = remoteSearchService.esDelProduct(id);

            //删除成功修改上下架状态为下架
            if (result.getCode() == Constants.SUCCESS){
                //根据spuId修改上下架状态
                spuInfoMerchantMapper.upperOrLowerShelves(id,publishStatus);
            }
        }else {
            //商品上架
            //根据spuId获取sku列表
            List<SkuInfo> skuInfos = getSkuInfoListBySpuId(id);

            //skuInfos --> skuEsModels
            List<SkuEsModel> skuEsModelList = transferSkuEsModelList(skuInfos,id);

            //远程调用批量将选中的spu下的sku信息添加到es中方法
            Result result = remoteSearchService.esSaveProduct(JSONObject.toJSONString(skuEsModelList));

            //批量添加成功修改上下架状态为上架
            if (result.getCode() == Constants.SUCCESS){
                //根据spuId修改上下架状态
                spuInfoMerchantMapper.upperOrLowerShelves(id,publishStatus);
            }
        }
    }

    /**
     * skuInfos --> skuEsModels
     * @param skuInfos
     * @param id
     * @return
     */
    private List<SkuEsModel> transferSkuEsModelList(List<SkuInfo> skuInfos, Long id) {
        //处理attrs
        //根据spuId查询spu属性
        List<PmsProductAttrValuePO> pmsProductAttrValuePOS = productAttrValueMapper.selectSearchableBySpuInfoId(id);
        List<Attrs> attrsList = pmsProductAttrValuePOS.stream().map(attrsValuePO -> {
            Attrs attrs = new Attrs();
            attrs.setAttrId(attrsValuePO.getAttrId());
            attrs.setAttrName(attrsValuePO.getAttrName());
            attrs.setAttrValue(attrsValuePO.getAttrValue());

            return attrs;
        }).collect(Collectors.toList());

        List<SkuEsModel> list = skuInfos.stream().map(skuInfo -> {
            SkuEsModel skuEsModel = new SkuEsModel();
            BeanUtils.copyProperties(skuInfo,skuEsModel);
            skuEsModel.setSkuPrice(skuInfo.getPrice());
            skuEsModel.setSkuImg(skuInfo.getSkuDefaultImg());
            skuEsModel.setHasStock(true);
            skuEsModel.setHotScore(Long.valueOf(100));

            PmsBrandPO pmsBrandPO = pmsBrandMapper.selectPmsBrandByBrandId(skuInfo.getBrandId());
            skuEsModel.setBrandId(pmsBrandPO.getBrandId());
            skuEsModel.setBrandName(pmsBrandPO.getName());
            skuEsModel.setBrandImg(pmsBrandPO.getLogo());

            PmsCategoryPO pmsCategoryPO = pmsCategoryMapper.selectPmsCategoryByCatId(skuInfo.getCatalogId());
            skuEsModel.setCatalogId(pmsCategoryPO.getCatId());
            skuEsModel.setCatalogName(pmsCategoryPO.getName());

            skuEsModel.setAttrs(attrsList);
            return skuEsModel;
        }).collect(Collectors.toList());

        return list;
    }

    /**
     * 根据spuId获取sku列表
     * @param id
     * @return
     */
    private List<SkuInfo> getSkuInfoListBySpuId(Long id) {
        SkuInfo skuInfo = new SkuInfo();
        skuInfo.setSpuId(id);
        List<SkuInfo> skuInfos = skuInfoMapper.selectPmsSkuInfoList(skuInfo);
        return skuInfos;
    }
}
